package controllers

import (
	"filfoxexporter/services"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
)

func Index(c *gin.Context) {
	c.HTML(http.StatusOK, "index.html", nil)
}

// func Message(c *gin.Context) {
// 	address := make([]string, 0)
// 	array := strings.Split(strings.ReplaceAll(c.PostForm("address"), " ", ""), "\r\n")
// 	for _, item := range array {
// 		if len(item) > 0 {
// 			address = append(address, item)
// 		}
// 	}

// 	ok, err := services.All(address)
// 	if err != nil {
// 		fmt.Printf("all service err: %v", err)
// 		c.String(http.StatusInternalServerError, "出错啦，错误是：%v", err)
// 		return
// 	}
// 	if !ok {
// 		fmt.Printf("all service !ok")
// 		c.String(http.StatusInternalServerError, "获取数据失败")
// 		return
// 	}

// 	addresses := make([]string, 0)
// 	for _, item := range address {
// 		addresses = append(addresses, fmt.Sprintf("http://%v/download/all-%v.csv", c.Request.Host, item))
// 	}

// 	c.JSON(http.StatusOK, map[string][]string{"addresses": addresses})
// }

func Message(c *gin.Context) {
	from := strings.ReplaceAll(c.PostForm("from"), "T", " ")
	to := strings.ReplaceAll(c.PostForm("to"), "T", " ")
	typo := c.PostForm("type")
	dur := c.PostForm("dur")
	duration, _ := strconv.ParseInt(dur, 10, 64)
	fmt.Println(from + to)

	address := make([]string, 0)
	array := strings.Split(strings.ReplaceAll(c.PostForm("address"), " ", ""), "\r\n")
	for _, item := range array {
		if len(item) > 0 {
			address = append(address, item)
		}
	}

	ok, err := services.Message(from, to, address, typo, duration)
	if err != nil {
		fmt.Printf("send service err: %v", err)
		c.String(http.StatusInternalServerError, "出错啦，错误是：%v", err)
		return
	}
	if !ok {
		fmt.Printf("send service !ok")
		c.String(http.StatusInternalServerError, "获取数据失败")
		return
	}

	addresses := make([]string, 0)
	for _, item := range address {
		addresses = append(addresses, fmt.Sprintf("http://%v/download/msg-%v.csv", c.Request.Host, item))
	}

	c.JSON(http.StatusOK, map[string][]string{"addresses": addresses})
}

func Trans(c *gin.Context) {
	from := strings.ReplaceAll(c.PostForm("from"), "T", " ")
	to := strings.ReplaceAll(c.PostForm("to"), "T", " ")
	typo := c.PostForm("type")
	dur := c.PostForm("dur")
	duration, _ := strconv.ParseInt(dur, 10, 64)

	fmt.Println(from + to)

	address := make([]string, 0)
	array := strings.Split(strings.ReplaceAll(c.PostForm("address"), " ", ""), "\r\n")
	for _, item := range array {
		if len(item) > 0 {
			address = append(address, item)
		}
	}

	ok, err := services.Trans(from, to, address, typo, duration)
	if err != nil {
		fmt.Printf("trans service err: %v", err)
		c.String(http.StatusInternalServerError, "出错啦，错误是：%v", err)
		return
	}
	if !ok {
		fmt.Printf("trans service !ok")
		c.String(http.StatusInternalServerError, "获取数据失败")
		return
	}

	addresses := make([]string, 0)
	for _, item := range address {
		addresses = append(addresses, fmt.Sprintf("http://%v/download/trans-%v.csv", c.Request.Host, item))
	}

	c.JSON(http.StatusOK, map[string][]string{"addresses": addresses})
}
